<?php
namespace jy_mayitwc;
defined('BY_JYA') or exit('error');
/*
*/
class member extends \plugin{
	public function addlog($postdata=array()){
		$log=$this->pdo_get('log',array('mid'=>$_SESSION['uid'],'passed'=>0));
		$data=array(
			'sn'=>'SN'.get_ordersn($this->tablename('log'),'sn'),
			'orderid'=>$postdata['bizcontent']['orderId'],
			'type'=>$postdata['type'],
			'operationtype'=>$postdata['operationtype'],
			'bizcontent'=>ijson_encode($postdata['bizcontent']),
			'response_data'=>$postdata['response_data'],
			'passed'=>$postdata['passed']?:0,
		);
		if($data['orderid']){
			$ordersn=pdo_getcolumn('jy_weishop_order',array('id'=>$data['orderid']),'ordersn');
			$data['ordersn']=$ordersn;
		}
		if($log['id']){
			$this->pdo_update('log',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['id']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['mid']=$_SESSION['uid'];
			$data['createtime']=TIMESTAMP;
			$this->pdo_insert('log',$data);
		}
		return false;
	}
	public function check_passed($postdata=array()){
		$passed=0;
		$res=$this->model('model')->person_passed(array('certifyId'=>$postdata['certifyId'],'outerOrderNo'=>$postdata['ordersn']));
		if($res['passed']=='T'){
			$update=array(
				'realname'=>$postdata['realname'],
				'cardnum'=>$postdata['cardnum'],
				'isrealname'=>1,
			);
			$passed=1;
			pdo_update('core_members',$update,array('id'=>$_SESSION['uid']));
			$this->pdo_update('log',array('passed'=>1),array('certifyId'=>$postdata['certifyId'],'mid'=>$_SESSION['uid']));
		}
		return $passed;
	}
	public function up_member($orderid=0){
		$isadd=pdo_get('jy_mayitwc_member',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderid));
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		$member=pdo_get('core_members',array('id'=>$order['mid']));
		$cardinfo=json_decode($member['cardinfo'],true);
		if(!$member['cardnum'] || !$member['realname'] || !$member['isrealname']){
			exi($member['cardnum'].'该用户未先实名认证。无法上链！'.$member['realname'],'error');
		}
		$data=array(
			'orderid'=>$orderid,
			'mid'=>$order['mid'],
			'usertype'=>1,
			'username'=>$member['realname'],
			'userphonenumber'=>$member['mobile'],
			'alipayuid'=>'',
		);
		if(!$isadd['id']){
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_mayitwc_member',$data);
			$isadd['id']=pdo_insertid();
		}else{
			pdo_update('jy_mayitwc_member',$data,array('id'=>$isadd['id']));
		}
		$bizContent=$this->memberinfo($isadd['id']);
		$res=$this->model('model')->upinfo($bizContent,'user','',true);
		$update=array(
			'biz_content'=>ijson_encode($bizContent),
			'response_data'=>ijson_encode($res),
			'status'=>1,
			'uptime'=>TIMESTAMP
		);
		pdo_update('jy_mayitwc_member',$update,array('id'=>$isadd['id']));
		return true;
	}
	public function orderinfo($ordersn='',$aliapp_id=0){
		if(!$ordersn){
			exi('请输入订单号！','error');
		}
		$aliapp_id=$aliapp_id?:$this->setting['aliapp_id'];
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ordersn'=>$ordersn));
		if(!$order['id']){
			exi('订单不存在！','error');
		}
		if(!$order['esign_flow_id']){
			exi('该订单没有e签宝签约！不能上链！','error');
		}
		$order['address']=json_decode($order['address'],true);
		if($order['address'] && $order['address']['name']){	
			$address=$order['address'];
		}else{
			$address=$order['addressid'];
		}
		$address=model('core/member')->get_address_str($address);
		$api_data=json_decode($order['api_data'],true);
		if((!$api_data['hash'] || !$api_data['leaseContractUrl']) && $aliapp_id){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$aliapp_id,'uniacid'=>$_SESSION['uniacid']));
			$setting=array(
				'alipay_appid'=>$wxapp['ali_appid'],
				'alipay_private_key'=>$wxapp['alipay_private_key'],
				'alipay_public_key'=>$wxapp['alipay_public_key'],
			);
			$ret=model('jy_alipay/alipay',$setting)->ehtong_signdocs($order['esign_flow_id']);
			if($ret[0] && $ret[0]['file_url']){
				$ext=substr(strrchr($ret['file_url'],'.'),1);
				$docsstr=@file_get_contents($ret['file_url']);
				$api_data['hash']=hash("sha256", $docsstr);
				$api_data['leaseContractUrl']=cfc('files')->source_save($docsstr,'other',$ext);
				pdo_update('jy_weishop_order',array('api_data'=>ijson_encode($api_data)),array('id'=>$order['id']));
			}else{
				exi('签约合同失败！FID='.$order['esign_flow_id'].','.ijson_encode($ret),'error');
			}
		}
		$pcd=$order['address']['pcd'];
		if($pcd){
			$pcd=json_decode($pcd,true);
			$provinceCode=$this->get_area_code($pcd,1);
			$cityCode=$this->get_area_code($pcd,2);
			$districtCode=$this->get_area_code($pcd,3);
		}
		$bizContent=array(
			'orderId'=>$order['id'],
			'orderCreateTime'=>date('Y-m-d H:i:s',$order['createtime']),
			'orderPayTime'=>date('Y-m-d H:i:s',$order['paytime']),
			'orderPayId'=>$order['pay_id'],
			'orderPayType'=>3,
			'depositFree'=>$api_data['ya_price']?$api_data['ya_price']*10000:0,
			'acutalPreAuthFree'=>$order['price']*10000,
			'rentTerm'=>$api_data['allqi']?:1,
			'rentPricePerMonth'=>$api_data['total_price']*10000,
			'buyOutPrice'=>$api_data['duan_price']*10000,
			'hash'=>$api_data['hash'],
			//'leaseServiceFileTxHash'=>$api_data['leaseServiceFileTxHash'],
			'leaseContractUrl'=>tomedia($api_data['leaseContractUrl']),
			'storeType'=>1,
			'userAddress'=>$address,
			'provinceCode'=>$provinceCode['area_code'],
			'cityCode'=>$cityCode['area_code'],
			'districtCode'=>$districtCode['area_code'],
			'dueMode'=>2,
			'totalRentMoney'=>$api_data['allprice']?$api_data['allprice']*10000:100,
			'yidunScore'=>$api_data['yidunScore'],
		);
		return $bizContent;
	}
	public function goodsinfo($goodsid=0,$aliapp_id=0){
		if(!$goodsid){
			exi('请选择商品！','error');
		}
		$aliapp_id=$aliapp_id?:$this->setting['aliapp_id'];
		$goods=pdo_get('jy_mayitwc_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodsid));
		$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
		$bizContent=array(
			'productId'=>$goodsinfo['id'],
			'productVersion'=>'1',
			'productName'=>$goodsinfo['title'],
			'productPrice'=>$goodsinfo['price']*10000,
			'mainClass'=>$goods['mainclass'],
			'subClass'=>$goods['subclass'],
			'supplierName'=>$goodsinfo['title'],
			'supplierId'=>$this->setting['supplierid'],
			'installPrice'=>0.00,
			'productOrigin'=>2,
			'realStock'=>$goodsinfo['total'],
			'estimatedShipment'=>intval($goodsinfo['salenum']),
			'productDetailInfo'=>$goodsinfo['subtitle']?:$goodsinfo['title'],
			'productUrl'=>custom_url('jy_zushou/goods.detail//id='.$goodsinfo['id'],true),
			'productBrand'=>$goods['productBrand'],
			'productModel'=>$goods['productModel'],
			'depositPrice'=>intval($goods['depositPrice'])*10000,
		);
		return $bizContent;
	}
	public function memberinfo($memberid=0,$aliapp_id=0){
		if(!$memberid){
			exi('请选择用户！','error');
		}
		$aliapp_id=$aliapp_id?:$this->setting['aliapp_id'];
		$member=pdo_get('jy_mayitwc_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$memberid));
		$memberinfo=pdo_get('core_members',array('id'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
		$cardinfo=json_decode($memberinfo['cardinfo'],true);
		if(!$memberinfo['cardnum'] || !$memberinfo['realname'] || !$memberinfo['isrealname']){
			exi('该用户未先实名认证！无法上链！','error');
		}
		$lease_corp_image=tomedia($this->setting['lease_corp_image']);
		$lease_corp_image=@file_get_contents($lease_corp_image);
		$businesslicenseHash=hash("sha256", $lease_corp_image);
		$bizContent=array(
			'orderId'=>$member['orderid'],
			'loginId'=>$memberinfo['id'],
			'loginType'=>1,
			'loginTime'=>date('Y-m-d H:i:s'),
			'userName'=>$memberinfo['realname'],
			'userId'=>$memberinfo['cardnum'],
			'userImageUrl'=>$cardinfo['image'],
			'userBackImageUrl'=>$cardinfo['backimage'],
			//'userImageHash'=>$cardinfo['imagehash'],
			//'userBackImageHash'=>$cardinfo['backimagehash'],
			'userPhoneNumber'=>$memberinfo['mobile'],
			'userType'=>3,
			'alipayUID'=>$memberinfo['mobile'],
			'leaseCorpName'=>$this->setting['lease_corp_name'],
			'leaseCorpId'=>$this->setting['lease_corp_id'],
			'leaseCorpOwnerName'=>$this->setting['lease_corp_owner_name'],
			'lesseeType'=>2,
		);
		return $bizContent;
	}
	public function ordergoodsinfo($orderid=0,$aliapp_id=0){
		if(!$orderid){
			exi('请选择订单！','error');
		}
		$aliapp_id=$aliapp_id?:$this->setting['aliapp_id'];
		$goods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderid));
		$productInfo=array();
		foreach($goods as $gv){
			$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']));
			$isshang=pdo_get('jy_mayitwc_goods',array('goodsid'=>$gv['goodsid']));
			if(!$isshang['status']){
				exi($goodsinfo['title'].'-商品还未上链！请先前往商品上链！','error');
			}
			$productInfo[]=array(
				'productId'=>$goodsinfo['id'],
				'productVersion'=>'1',
				'productName'=>$goodsinfo['title'],
				'productImeiId'=>$goodsinfo['goodssn']?:'无',
				'productPrice'=>$gv['price']*10000,
				'productNumber'=>intval($gv['total']),
				'supplierId'=>$this->setting['supplierid'],
			);
		}
		$bizContent=array(
			'productInfo'=>$productInfo,
			'orderId'=>$orderid,
			'leaseId'=>$this->setting['supplierid'],
		);
		return $bizContent;
	}
	public function get_area_code($pcd,$level=1){
		if($level==1){
			$title=$pcd['province'];
			$levelpcd=array(
				'province'=>$pcd['province']
			);
		}elseif($level==2){
			$title=$pcd['city'];
			$levelpcd=array(
				'province'=>$pcd['province'],
				'city'=>$pcd['city'],
			);
		}elseif($level==3){
			$title=$pcd['district'];
			$levelpcd=array(
				'province'=>$pcd['province'],
				'city'=>$pcd['city'],
				'district'=>$pcd['district'],
			);
		}elseif($level==4){
			$title=$pcd['street'];
			$levelpcd=array(
				'province'=>$pcd['province'],
				'city'=>$pcd['city'],
				'district'=>$pcd['district'],
				'street'=>$pcd['street'],
			);
		}
		if(!$levelpcd){
			return false;
		}
		$address=$pcd['province'];
		if($pcd['city']){
			$address.=' '.$pcd['city'];
		}
		if($pcd['district']){
			$address.=' '.$pcd['district'];
		}
		if($pcd['street']){
			$address.=' '.$pcd['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		$data=array(
			'area_code'=>$area_code,
			'address'=>$address,
			'title'=>$title,
		);
		return $data;
	}
}